package com.yzfarm.biujoke.biujoke.mvp.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.yzfarm.biujoke.biujoke.R;
import com.yzfarm.biujoke.biujoke.mvp.base.BaseActivity;
import com.yzfarm.biujoke.biujoke.mvp.model.datamanager.RetrofitManager;
import com.yzfarm.biujoke.biujoke.mvp.model.http.MeiNvApis;
import com.yzfarm.biujoke.biujoke.mvp.presenter.BrowseBiuPresenter;
import com.yzfarm.biujoke.biujoke.ui.adapter.SlidePagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hanhy on 2016/10/9.
 */

public class BrowseBiuActivity extends BaseActivity<BrowseBiuPresenter> {

    Toolbar mToolbar;
    String url;
    String picName;
    TextView textView;
    List<String> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        textView = (TextView) findViewById(R.id.count);
        setToolBar(mToolbar,picName);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BrowseBiuActivity.this.finish();
            }
        });

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.browsebiu_activity;
    }

    @Override
    protected void initEventAndData() {

        ViewPager pager = (ViewPager)findViewById(R.id.pager);
        url = getIntent().getStringExtra("picUrl");
        picName = getIntent().getStringExtra("picName");
        final SlidePagerAdapter pagerAdapter =
                new SlidePagerAdapter(getSupportFragmentManager());
         list = new ArrayList<String>();
        pagerAdapter.addAll(list);
        // set pictures
        RetrofitManager.getInstance().getMeiNvApis().getOneMeiNvs(url, new MeiNvApis.MeinvApiCallback<List<String>>() {
            @Override
            public void onSuccess(final List<String> list) {

                BrowseBiuActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        BrowseBiuActivity.this.list = list;
                        pagerAdapter.addAll(list);
                        pagerAdapter.notifyDataSetChanged();
                    }
                });

            }
        });

        pager.setAdapter(pagerAdapter);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                textView.setText((position+1)+"/"+list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        CirclePageIndicator mPageIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
//        mPageIndicator.setViewPager(pager);

    }
}
